package com.wgchao.identityrightsmanagement.application.service;

import com.wgchao.identityrightsmanagement.domain.resource.model.ProjectResources;
import com.wgchao.identityrightsmanagement.domain.resource.model.ProjectResourcesCreator;
import com.wgchao.identityrightsmanagement.domain.resource.model.ProjectResourcesUpdator;
import com.wgchao.identityrightsmanagement.domain.resource.repository.ProjectRepository;
import com.wgchao.identityrightsmanagement.infrastructure.exception.ObjectNotExistException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @ClassName: ProjectApplicationService
 * @author: wgchao
 * @createTime: 2020/6/11 10:35 AM
 * @Description: 项目资源管理
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ProjectApplicationService {
    @Autowired
    private ProjectRepository projectRepository;
    @Autowired
    private ApplicationEventPublisher publisher;

    //添加项目
    public void addProjectResources(ProjectResourcesCreator creator, String operator){
        ProjectResources projectResources = ProjectResources.addProject(creator);
        projectRepository.add(projectResources, operator);
    }

    //修改项目
    public void modifyProjectInfo(String proMark, ProjectResourcesUpdator updator, String operator){
        ProjectResources projectResources = projectRepository.findById(proMark);
        if(projectResources == null){
            throw new ObjectNotExistException("项目不存在");
        }
        projectResources.modifyProjectInfo(updator);
        projectRepository.add(projectResources, operator);
    }

    //删除项目
    public void removeProject(String proMark, String operator){
        ProjectResources projectResources = projectRepository.findById(proMark);
        if(projectResources == null){
            throw new ObjectNotExistException("项目不存在");
        }
        projectRepository.remove(projectResources, operator);

        //TODO 通知项目资源已删除
        publisher.publishEvent(null);
        
    }

    /**
     * 停用项目资源
     * @param proMark
     * @param operator
     */
    public void disable(String proMark, String operator){
        ProjectResources projectResources = projectRepository.findById(proMark);
        if(projectResources == null){
            throw new ObjectNotExistException("项目不存在");
        }
        projectResources.disable();
        projectRepository.add(projectResources, operator);
    }

    /**
     * 启用项目资源
     * @param proMark
     * @param operator
     */
    public void regain(String proMark, String operator){
        ProjectResources projectResources = projectRepository.findById(proMark);
        if(projectResources == null){
            throw new ObjectNotExistException("项目不存在");
        }
        projectResources.regain();
        projectRepository.add(projectResources, operator);
    }
}
